package com.film.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.film.dao.OrderDao;
import com.film.dao.impl.OrderDaoImpl;
import com.film.domain.FilmOffice;
import com.film.domain.FilmOrder;
import com.film.domain.FilmSeat;
import com.film.domain.FilmShowings;
import com.film.domain.User;
import com.film.domain.UserOrder;
import com.film.service.OrderService;
import com.film.util.MyUtil;

public class OrderServiceImpl implements OrderService{
	
	private OrderDao orderDao = new OrderDaoImpl();

	@Override
	public List<FilmOrder> selectAllOrder(int uid) {
		return orderDao.selecAllOrder(uid);
	}
	
	@Override
	public List<UserOrder> arrangeList(List<FilmOrder> allUserOrders) {
		List<UserOrder> resultList = null;
		if(allUserOrders.size() > 0) {
			resultList = new ArrayList<>();
			//取出第一条订单的订单号
			String orderNum = allUserOrders.get(0).getOrderNum();
			//取出第一条订单的创建时间
			String createTime = allUserOrders.get(0).getCreateTime();
			//取出第一条订单的场次id
			int showingsId = allUserOrders.get(0).getFilmShowings().getShowingsId();
			boolean flag = false;
			UserOrder userOrder = new UserOrder();
			List<FilmSeat> seats = new ArrayList<>();
			for (int i = 0; i < allUserOrders.size(); i++) {
				FilmOrder filmOrder = allUserOrders.get(i);
				String oNum = filmOrder.getOrderNum();
				String cTime = filmOrder.getCreateTime();
				int sId = filmOrder.getFilmShowings().getShowingsId();
				if(cTime.equals(createTime) && oNum.equals(orderNum) && showingsId == sId) {
					toObject(userOrder, filmOrder);
					seats.add(filmOrder.getFilmSeat());
				}else {
					userOrder.setSeats(seats);
					resultList.add(userOrder);
					userOrder = new UserOrder();
					seats = new ArrayList<>();
					toObject(userOrder, filmOrder);
					seats.add(filmOrder.getFilmSeat());
				}
				if(filmOrder == allUserOrders.get(allUserOrders.size()-1)) {
					userOrder.setSeats(seats);
					resultList.add(userOrder);
				}
				//比较下一条
				orderNum = filmOrder.getOrderNum();
				createTime = filmOrder.getCreateTime();
				showingsId = allUserOrders.get(i).getFilmShowings().getShowingsId();
			}
			
		}
		return resultList;
	}
	
	private UserOrder toObject(UserOrder userOrder,FilmOrder filmOrder) {
		userOrder.setDatetime(filmOrder.getCreateTime());
		String[] dateArr = this.splitTime(filmOrder.getCreateTime());
		userOrder.setOrderNum(filmOrder.getOrderNum());
		userOrder.setDate(dateArr[0]);
		userOrder.setTime(dateArr[1]);
		FilmOffice office = filmOrder.getFilmShowings().getFilmOffice();
		userOrder.setOffice(office);
		userOrder.setShowings(filmOrder.getFilmShowings());
		userOrder.setOrderFlag(filmOrder.getOrderFlag());
		userOrder.setUser(filmOrder.getUser());
		return userOrder;
	}
	
	
	
	//用来处理时间，将其分成日期和具体时间
	private String[] splitTime(String dateTime) {
		return dateTime.split(" ");
	}

	@Override
	public String createOrder(int showingsId, int uid, List<Integer> seatIds) {
		//生成一条15位随机订单号
		String orderNum = MyUtil.generateOrderNumber();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = dateFormat.format(new Date());
		for (int i = 0; i < seatIds.size(); i++) {
			FilmOrder order = new FilmOrder();
			order.setOrderNum(orderNum);
			order.setFilmShowings(new FilmShowings(showingsId));
			order.setUser(new User(uid));
			order.setFilmSeat(new FilmSeat(seatIds.get(i)));
			order.setCreateTime(date);
			orderDao.insertOrder(order);
		}
		return orderNum;
	}

	@Override
	public List<FilmOrder> selectOrder(String orderNum) {
		return orderDao.selectOrder(orderNum);
	}

	@Override
	public int removeOrder(String orderNum) {
		return orderDao.removeOrder(orderNum);
	}

	@Override
	public int updateOrderStatus(String orderNum, int orderStatusCode) {
		return orderDao.updateOrderStatus(orderNum, orderStatusCode);
	}

	@Override
	public List<FilmOrder> selectAllOrder() {
		return orderDao.selectAllOrder();
	}
	

}
